package com.yuexinduo.education.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.yuexinduo.education.R;
import com.yuexinduo.education.ui.H5Activity;

public class AgreementDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private OnItemClickListener listener;
    private TextView notTv, agreeTv, topic;
    private SpannableStringBuilder spannable,spannablev2;

    private int endIndex,endIndexv2;
    private int startIndex,startIndexv2;
    private String titlev2="和《隐私政策》各项条款。当您点击同意,并开始使用产品或服务,即表示您已经理解并同意该条款。";
    private String title="欢迎使悦薪多教育APP！在您使用时,需要连接数据网络或WLAN网络,产生的流量费用请咨询当地运营商。我们非常重视您的隐私保护和个人信息保护。在您使用悦薪多教育APP服务前,请认真阅读《服务协议》";


    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void OnItemClick(int action);
    }
    public AgreementDialog(@NonNull Context context) {
        super(context);
        this.context=context;
        this.spannable = new SpannableStringBuilder(title);
        this.spannablev2 = new SpannableStringBuilder(titlev2);
        if (!TextUtils.isEmpty(title)) {
            startIndex = title.indexOf("《");
            endIndex = title.lastIndexOf("》")+1;
        }

        this.spannable.setSpan(new TextClick(),startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (!TextUtils.isEmpty(titlev2)) {
            startIndexv2 = titlev2.indexOf("《");
            endIndexv2 = titlev2.lastIndexOf("》")+1;
        }

        this.spannablev2.setSpan(new TextClick(),startIndexv2, endIndexv2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_agreement);

        notTv = findViewById(R.id.not);
        agreeTv = findViewById(R.id.agree);
        topic = findViewById(R.id.topic);
        topic.setMovementMethod(LinkMovementMethod.getInstance());
        topic.setText(spannable);
        topic.append(spannablev2);

        notTv.setOnClickListener(this);
        agreeTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.not:
                dismiss();
                if (listener != null) {
                    listener.OnItemClick(0);
                }
                break;
            case R.id.agree:
                dismiss();
                if (listener != null) {
                    listener.OnItemClick(1);
                }
                break;
            default:
                break;
        }
    }

    /**** 部分文字点击事件*/
    private class TextClick extends ClickableSpan {
        @Override
        public void onClick(View view) {
            TextView tv = (TextView) view;
            String msg = tv.getText().subSequence(tv.getSelectionStart(),tv.getSelectionEnd()).toString();
            if(msg.equals("《隐私政策》")){
                context.startActivity(new Intent(context,H5Activity.class)
                        .putExtra("Type","2"));

            }else if(msg.equals("《服务协议》")){
                context.startActivity(new Intent(context, H5Activity.class)
                        .putExtra("Type","3"));
            }
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(Color.parseColor("#0082FF"));   //自定义颜色值
            ds.setUnderlineText(false); //设置没有下划线
        }
    }
}
